package lion.bugrambo.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import lion.framework.core.db.DB;
import lion.framework.core.db.DataTable;
import lion.framework.core.db.paging.SQLPagingFactory;
import lion.framework.core.db.SessionFactory;
import lion.dev.lang.BeanUtil;
import lion.dev.lang.Lang;
import lion.dev.lang.MapJ;

public class Bug implements Serializable{

	private static final long serialVersionUID = 1L;
	public static final String TABLE_NAME = "Bug";

	private static String _SAVESQL="insert into  " + TABLE_NAME + "(title,description,createBuild,creator,createTime,solveAccId,solveBuild,solveType,solveTime,bugStatus,closeAccId,closeTime,owner,logs,level,projectId,attachs,modelId)values(:title,:description,:createBuild,:creator,:createTime,:solveAccId,:solveBuild,:solveType,:solveTime,:bugStatus,:closeAccId,:closeTime,:owner,:logs,:level,:projectId,:attachs,:modelId)";
	private static String _UPDATESQL="update " + TABLE_NAME + " set title=:title,description=:description,createBuild=:createBuild,creator=:creator,createTime=:createTime,solveAccId=:solveAccId,solveBuild=:solveBuild,solveType=:solveType,solveTime=:solveTime,bugStatus=:bugStatus,closeAccId=:closeAccId,closeTime=:closeTime,owner=:owner,logs=:logs,level=:level,projectId=:projectId,attachs=:attachs,modelId=:modelId where id=:id";
	private static String _DELETESQL="delete from " + TABLE_NAME + " where id=? ";
	private static String _DELETEALLSQL="delete from " + TABLE_NAME + " where id in(%s) ";
	private static String _GETSQL="select * from " + TABLE_NAME + " where id=? ";
	private static String _LISTSQL="select * from " + TABLE_NAME + " ";
	private static String _COUNTSQL="select count(*) from " + TABLE_NAME + " ";

	
	private int id;
	private String title;
	private String description;
	private String createBuild;
	private int creator;
	private Date createTime;
	private int solveAccId;
	private String solveBuild;
	private int solveType;
	private Date solveTime;
	private int bugStatus;
	private int closeAccId;
	private Date closeTime;
	private int owner;
	private String logs;
	private int level;
	private int projectId;
	private String attachs;
	private int modelId;
	
	public void setId(int id){this.id=id;}
	public int getId(){return this.id;}
	public void setTitle(String title){this.title=title;}
	public String getTitle(){return this.title;}
	public void setDescription(String description){this.description=description;}
	public String getDescription(){return this.description;}
	public void setCreateBuild(String createBuild){this.createBuild=createBuild;}
	public String getCreateBuild(){return this.createBuild;}
	public void setCreator(int creator){this.creator=creator;}
	public int getCreator(){return this.creator;}
	public void setCreateTime(Date createTime){this.createTime=createTime;}
	public Date getCreateTime(){return this.createTime;}
	public void setSolveAccId(int solveAccId){this.solveAccId=solveAccId;}
	public int getSolveAccId(){return this.solveAccId;}
	public void setSolveBuild(String solveBuild){this.solveBuild=solveBuild;}
	public String getSolveBuild(){return this.solveBuild;}
	public void setSolveType(int solveType){this.solveType=solveType;}
	public int getSolveType(){return this.solveType;}
	public void setSolveTime(Date solveTime){this.solveTime=solveTime;}
	public Date getSolveTime(){return this.solveTime;}
	public void setBugStatus(int bugStatus){this.bugStatus=bugStatus;}
	public int getBugStatus(){return this.bugStatus;}
	public void setCloseAccId(int closeAccId){this.closeAccId=closeAccId;}
	public int getCloseAccId(){return this.closeAccId;}
	public void setCloseTime(Date closeTime){this.closeTime=closeTime;}
	public Date getCloseTime(){return this.closeTime;}
	public void setOwner(int owner){this.owner=owner;}
	public int getOwner(){return this.owner;}
	public void setLogs(String logs){this.logs=logs;}
	public String getLogs(){return this.logs;}
	public void setLevel(int level){this.level=level;}
	public int getLevel(){return this.level;}
	public void setProjectId(int projectId){this.projectId=projectId;}
	public int getProjectId(){return this.projectId;}
	public void setAttachs(String attachs){this.attachs=attachs;}
	public String getAttachs(){return this.attachs;}
	public void setModelId(int modelId){this.modelId=modelId;}
	public int getModelId(){return this.modelId;}

	
	public void save() {

		DB db = SessionFactory.getCurrentDB();
		this.save(db);
	}

	public void save(DB db) {

		DataTable dt = db.save(_SAVESQL, this.valueMap());
		if (dt.getRowCount() > 0) {
			MapJ row = dt.getRow(0);
			this.id = row.getInt(row.getKey(0));
		}
	}

	public void update() {

		DB db = SessionFactory.getCurrentDB();
		this.update(db);

	}

	public void update(DB db) {

		db.update(_UPDATESQL, this.valueMap());
	}

	public void delete() {

		DB db = SessionFactory.getCurrentDB();
		this.delete(db);
	}

	public void delete(DB db) {

		db.update(_DELETESQL, this.getId());
	}

	public static void delete(DB db, int... ids) {

		if (ids == null || ids.length == 0) { return; }
		String sql = String.format(_DELETEALLSQL, Lang.join(ids));
		db.update(sql);
	}

	public static void delete(int... ids) {

		delete(SessionFactory.getCurrentDB(), ids);
	}
	
	public static Bug load(int id) {

		DB db = SessionFactory.getCurrentDB();
		return load(id, db);
	}

	public static Bug load(int id, DB db) {

		MapJ row = db.queryOneRow(_GETSQL, id);

		if(row == null){return null;}
		return Bug.create(row);

	}
	
	public static Bug load(String where) {

		DB db = SessionFactory.getCurrentDB();
		return load(where, db);
	}

	public static Bug load(String where, DB db) {

		return load(where, null, db);
	}

	public static Bug load(String where,MapJ param, DB db) {

		String sql = _LISTSQL + (StringUtils.isBlank(where) ? "" : where);

		MapJ row = db.queryOneRow(sql,param);
		if(row == null){return null;}

		return Bug.create(row);
	}

	public static int count() {

		return count("");
	}

	public static int count(DB db) {

		return count("", db);
	}

	public static int count(String where) {

		return count(where, SessionFactory.getCurrentDB());
	}

	public static int count(String where, MapJ param) {

		return count(where, param, SessionFactory.getCurrentDB());
	}

	public static int count(String where, DB db) {

		return count(where, null, db);
	}

	public static int count(String where, MapJ param, DB db) {

		String sql = _COUNTSQL + (StringUtils.isBlank(where) ? "" : " " + where);
		return db.queryInt(sql, param);

	}
	public static Bug create(MapJ map) {

		Bug o = new Bug();
		try {
			BeanUtil.populate(o, map);
		} catch (Exception e) {
		}

		return o;
	}

	public static List<Bug> list(int size) {

		DB db = SessionFactory.getCurrentDB();
		return list(size, db);
	}

	public static List<Bug> list(int size, DB db) {

		return list(size, null, db);
	}

	public static List<Bug> list(int size, String where) {

		return list(size, where, SessionFactory.getCurrentDB());
	}

	public static List<Bug> list(int size, String where, DB db) {

		return list(1, size, where, db);
	}

	public static List<Bug> list(int size, String where, MapJ param, DB db) {

		return list(1, size, where, param, db);
	}

	public static List<Bug> list(int start, int size) {

		return list(start, size, SessionFactory.getCurrentDB());
	}

	public static List<Bug> list(int start, int size, DB db) {

		return list(start, size, null, db);
	}

	public static List<Bug> list(int start, int size, String where) {

		return list(start, size, where, SessionFactory.getCurrentDB());
	}

	public static List<Bug> list(int start, int size, String where, DB db) {

		return list(start, size, where, null, db);
	}

	public static List<Bug> list(int start, int size, String where, MapJ param, DB db) {

		String sql = SQLPagingFactory.paging(db.getType(), _LISTSQL + " " + (StringUtils.isBlank(where) ? "" : where), start, size);

		DataTable dt = null;
		if (param == null) {
			dt = db.query(sql);
		} else {
			dt = db.query(sql, param);
		}

		List<Bug> result = new ArrayList<>();

		for (MapJ row : dt.getRows()) {
			result.add(Bug.create(row));
		}

		return result;
	}

	public MapJ valueMap() {

		MapJ mapJ = new MapJ();
		
		mapJ.put("id", this.id);
		mapJ.put("title", this.title);
		mapJ.put("description", this.description);
		mapJ.put("createBuild", this.createBuild);
		mapJ.put("creator", this.creator);
		mapJ.put("createTime", this.createTime);
		mapJ.put("solveAccId", this.solveAccId);
		mapJ.put("solveBuild", this.solveBuild);
		mapJ.put("solveType", this.solveType);
		mapJ.put("solveTime", this.solveTime);
		mapJ.put("bugStatus", this.bugStatus);
		mapJ.put("closeAccId", this.closeAccId);
		mapJ.put("closeTime", this.closeTime);
		mapJ.put("owner", this.owner);
		mapJ.put("logs", this.logs);
		mapJ.put("level", this.level);
		mapJ.put("projectId", this.projectId);
		mapJ.put("attachs", this.attachs);
		mapJ.put("modelId", this.modelId);
		
		return mapJ;
	}
}